﻿using System;
using System.Collections.Generic;
using System.Text;

using RaisingStudio.Data.Common;
using RaisingStudio.Data.Common.Factories;
using RaisingStudio.Data.Expressions;
using RaisingStudio.Data.Common.Builders.Expressions;
using RaisingStudio.Data.Common.Managers;

namespace RaisingStudio.Data.Providers.Adapters
{
    /// <summary>
    /// dataset DataAdapter.
    /// </summary>
    public partial class DataSetAdapter : DataAdapterBase, RaisingStudio.Data.Common.IDataSetAdapter
    {
        #region static member
        /// <summary>
        /// Sets Command current value.
        /// </summary>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="values">data value array.</param>
        /// <param name="command">Command.</param>
        public static System.Data.Common.DataTableMapping CopyTableMapping(System.Data.Common.DataTableMapping dataTableMapping)
        {
            System.Data.Common.DataTableMapping dataTableMappingCopy = new System.Data.Common.DataTableMapping(dataTableMapping.SourceTable, dataTableMapping.DataSetTable);
            foreach (System.Data.Common.DataColumnMapping  dataColumnMapping in dataTableMapping.ColumnMappings)
            {
                System.Data.Common.DataColumnMapping dataColumnMappingCopy = new System.Data.Common.DataColumnMapping(dataColumnMapping.SourceColumn, dataColumnMapping.DataSetColumn);
                dataTableMappingCopy.ColumnMappings.Add(dataColumnMappingCopy);
            }
            return dataTableMappingCopy;
        }
        #endregion

        #region Common property 
        protected System.Type dataSetType;
        /// <summary>
        /// dataset Type.
        /// </summary>
        public System.Type DataSetType
        {
            get
            {
                return this.dataSetType;
            }
        }

        protected string definitionName;
        /// <summary>
        /// dataset definition name.
        /// </summary>
        public string DefinitionName
        {
            get
            {
                return this.definitionName;
            }
        }


        protected DataAdapterFactory dataAdapterFactory;
        /// <summary>
        /// DataAdapter factory.
        /// </summary>
        public DataAdapterFactory DataAdapterFactory
        {
            get
            {
                return this.dataAdapterFactory;
            }
        }

        protected Dictionary<string, System.Data.IDbDataAdapter> defaultDataAdapters;
        /// <summary>
        /// data adapter dictionary.
        /// </summary>
        public Dictionary<string, System.Data.IDbDataAdapter> DefaultDataAdapters
        {
            get
            {
                if (this.defaultDataAdapters == null)
                {
                    this.defaultDataAdapters = CreateDataAdapters();
                }
                return this.defaultDataAdapters;
            }
        }

        protected RaisingStudio.Data.Common.Builders.DataSetCommandBuilder commandBuilder;
        /// <summary>
        /// Command builder.
        /// </summary>
        public RaisingStudio.Data.Common.Builders.DataSetCommandBuilder CommandBuilder
        {
            get
            {
                if (this.commandBuilder == null)
                {
                    this.commandBuilder = CreateCommandBuilder();
                }
                return this.commandBuilder;
            }
        }
                
        protected bool cloneDataSet = true;
        /// <summary>
        /// Create same structure of dataset.
        /// </summary>
        public bool CloneDataSet
        {
            get
            {
                return this.cloneDataSet;
            }
        }

        /// <summary>
        /// datatable type dictionary.
        /// </summary>
        protected Dictionary<string, System.Type> dataTableTypes;
        /// <summary>
        /// datatable definition dictionary.
        /// </summary>
        protected Dictionary<string, string> dataTableDefinitionNames;
        #endregion

        #region Constructor
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        public DataSetAdapter(System.Type dataSetType)
        {
            this.dataSetType = dataSetType;
            this.connection = ConnectionFactory.CreateConnection();
            DatabaseType databaseType;
            string providerName;
            ConnectionFactory.GetConnectionSetting(out databaseType, out providerName);
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
            this.dataAdapterFactory = new DataAdapterFactory(this.connection, this._databaseType, this._providerName);
            GenerateDataTableDefinitionNames();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        /// <param name="connection">database connection.</param>
        public DataSetAdapter(System.Type dataSetType, System.Data.IDbConnection connection)
        {
            this.dataSetType = dataSetType;
            this.connection = connection;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
            this.dataAdapterFactory = new DataAdapterFactory(this.connection);
            GenerateDataTableDefinitionNames();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        /// <param name="connection">database connection.</param>
        public DataSetAdapter(System.Type dataSetType, System.Data.IDbConnection connection, DatabaseType databaseType, string providerName)
        {
            this.dataSetType = dataSetType;
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
            this.dataAdapterFactory = new DataAdapterFactory(this.connection, this._databaseType, this._providerName);
            GenerateDataTableDefinitionNames();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        /// <param name="transactionManager">database transaction manager.</param>
        public DataSetAdapter(System.Type dataSetType, TransactionManager transactionManager, ILogManager logManager)
        {
            this.dataSetType = dataSetType;
            this.transactionManager = transactionManager;
            this.connection = transactionManager.Connection;
            this.dataAdapterFactory = new DataAdapterFactory(this.connection);
            GenerateDataTableDefinitionNames();
            this._logManager = logManager;
        }

        public DataSetAdapter(System.Type dataSetType, TransactionManager transactionManager, DatabaseType databaseType, string providerName, ILogManager logManager)
        {
            this.dataSetType = dataSetType;
            this.transactionManager = transactionManager;
            this.connection = transactionManager.Connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.dataAdapterFactory = new DataAdapterFactory(this.connection, this._databaseType, this._providerName);
            GenerateDataTableDefinitionNames();
            this._logManager = logManager;
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        /// <param name="definitionName">dataset definition name.</param>
        public DataSetAdapter(System.Type dataSetType, string definitionName)
        {
            this.dataSetType = dataSetType;
            this.definitionName = definitionName;
            this.connection = ConnectionFactory.CreateConnection();
            DatabaseType databaseType;
            string providerName;
            ConnectionFactory.GetConnectionSetting(out databaseType, out providerName);
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
            this.dataAdapterFactory = new DataAdapterFactory(this.connection, this._databaseType, this._providerName);
            GenerateDataTableDefinitionNames();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        /// <param name="definitionName">dataset definition name.</param>
        /// <param name="connection">database connection.</param>
        public DataSetAdapter(System.Type dataSetType, string definitionName, System.Data.IDbConnection connection)
        {
            this.dataSetType = dataSetType;
            this.definitionName = definitionName;
            this.connection = connection;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
            this.dataAdapterFactory = new DataAdapterFactory(this.connection);
            GenerateDataTableDefinitionNames();
        }

        public DataSetAdapter(System.Type dataSetType, string definitionName, System.Data.IDbConnection connection, DatabaseType databaseType, string providerName)
        {
            this.dataSetType = dataSetType;
            this.definitionName = definitionName;
            this.connection = connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.transactionManager = TransactionManagerFactory.CreateTransactionManager(this.connection);
            this.dataAdapterFactory = new DataAdapterFactory(this.connection, this._databaseType, this._providerName);
            GenerateDataTableDefinitionNames();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dataTableType">Type of data object.</param>
        /// <param name="definitionName">dataset definition name.</param>
        /// <param name="transactionManager">database transaction manager.</param>
        public DataSetAdapter(System.Type dataSetType, string definitionName, TransactionManager transactionManager, ILogManager logManager)
        {
            this.dataSetType = dataSetType;
            this.definitionName = definitionName;
            this.transactionManager = transactionManager;
            this.connection = transactionManager.Connection;
            this.dataAdapterFactory = new DataAdapterFactory(this.connection);
            GenerateDataTableDefinitionNames();
            this._logManager = logManager;
        }

        public DataSetAdapter(System.Type dataSetType, string definitionName, TransactionManager transactionManager, DatabaseType databaseType, string providerName, ILogManager logManager)
        {
            this.dataSetType = dataSetType;
            this.definitionName = definitionName;
            this.transactionManager = transactionManager;
            this.connection = transactionManager.Connection;
            this._databaseType = databaseType;
            this._providerName = providerName;
            this.dataAdapterFactory = new DataAdapterFactory(this.connection, this._databaseType, this._providerName);
            GenerateDataTableDefinitionNames();
            this._logManager = logManager;
        }
        #endregion

        #region Create data adapter dictionary
        /// <summary>
        /// Gets datatable Type.
        /// </summary>
        /// <param name="type">Type of data object.</param>
        /// <param name="typeName">Type name.</param>
        /// <returns>datatable Type.</returns>
        public static System.Type GetDataTableType(System.Type type, string typeName)
        {
#if USE_TYPEMANAGER
            return RaisingStudio.Reflection.TypeManager.GetType(type, typeName);
#else
            System.Reflection.Assembly assembly;
            if (typeName.IndexOf(",") >= 0)
            {
                string[] names = typeName.Split(',');
                assembly = System.Reflection.Assembly.LoadFrom(names[0]);
                typeName = names[1];
            }
            else
            {
                assembly = type.Assembly;
            }

            return assembly.GetType(typeName);
#endif
        }

        /// <summary>
        /// create datatable definition dictionary.
        /// </summary>
        protected virtual void GenerateDataTableDefinitionNames()
        {
            this.dataTableTypes = new Dictionary<string, System.Type>();
            this.dataTableDefinitionNames = new Dictionary<string, string>();
            System.Xml.XmlElement xmlElement;
            if (this.definitionName != null)
            {
                xmlElement = DefinitionManager.Instance.LoadXmlDocument(this.dataSetType, this.definitionName);
            }
            else
            {
                xmlElement = DefinitionManager.Instance.LoadXmlDocument(this.dataSetType);
            }
            foreach (System.Xml.XmlNode tablesXmlNode in xmlElement)
            {
                if (tablesXmlNode is System.Xml.XmlElement)
                {
                    System.Xml.XmlElement tablesXmlElement = tablesXmlNode as System.Xml.XmlElement;
                    if (tablesXmlElement.Name == "Tables")
                    {
                        foreach (System.Xml.XmlNode tableXmlNode in tablesXmlElement)
                        {
                            if (tableXmlNode is System.Xml.XmlElement)
                            {
                                System.Xml.XmlElement tableXmlElement = tableXmlNode as System.Xml.XmlElement;
                                string tableName = tableXmlElement.Attributes["TableName"].Value.Trim();
                                if (tableXmlElement.Attributes["Type"] != null)
                                {
                                    string typeName = tableXmlElement.Attributes["Type"].Value.Trim();
                                    // 通过type Create table.
                                    System.Type dataTableType = GetDataTableType(this.dataSetType, typeName);
                                    this.dataTableTypes.Add(tableName, dataTableType);

                                }
                                if (tableXmlElement.Attributes["DefinitionName"] != null)
                                {
                                    string definitionName = tableXmlElement.Attributes["DefinitionName"].Value.Trim();
                                    this.dataTableDefinitionNames.Add(tableName, definitionName);
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Create data adapter dictionary.
        /// </summary>
        /// <returns>data adapter dictionary.</returns>
        public virtual Dictionary<string, System.Data.IDbDataAdapter> CreateDataAdapters()
        {
            Dictionary<string, System.Data.IDbDataAdapter> dataAdapters = new Dictionary<string, System.Data.IDbDataAdapter>();
            System.Xml.XmlElement xmlElement;
            if (this.definitionName != null)
            {
                xmlElement = DefinitionManager.Instance.LoadXmlDocument(this.dataSetType, this.definitionName);
            }
            else
            {
                xmlElement = DefinitionManager.Instance.LoadXmlDocument(this.dataSetType);
            }
            foreach (System.Xml.XmlNode tablesXmlNode in xmlElement)
            {
                if (tablesXmlNode is System.Xml.XmlElement)
                {
                    System.Xml.XmlElement tablesXmlElement = tablesXmlNode as System.Xml.XmlElement;
                    if (tablesXmlElement.Name == "Tables")
                    {
                        foreach (System.Xml.XmlNode tableXmlNode in tablesXmlElement)
                        {
                            if (tableXmlNode is System.Xml.XmlElement)
                            {
                                System.Xml.XmlElement tableXmlElement = tableXmlNode as System.Xml.XmlElement;
                                string tableName = tableXmlElement.Attributes["TableName"].Value.Trim();
                                if (tableXmlElement.Attributes["Type"] != null)
                                {
                                    string typeName = tableXmlElement.Attributes["Type"].Value.Trim();
                                    // 通过type Create table.
                                    System.Type dataTableType = GetDataTableType(this.dataSetType, typeName);

                                    if (tableXmlElement.Attributes["DefinitionName"] != null)
                                    {
                                        string definitionName = tableXmlElement.Attributes["DefinitionName"].Value.Trim();
                                        // Create DataAdapter.
                                        System.Data.IDbDataAdapter dataAdapter = CreateDataAdapter(dataTableType, definitionName);
                                        // Add DataAdapter.
                                        dataAdapters.Add(tableName, dataAdapter);
                                    }
                                    else
                                    {
                                        // Create DataAdapter.
                                        System.Data.IDbDataAdapter dataAdapter = CreateDataAdapter(dataTableType);
                                        // Add DataAdapter.
                                        dataAdapters.Add(tableName, dataAdapter);
                                    }
                                }
                                else
                                {
                                    if (tableXmlElement.Attributes["DefinitionName"] != null)
                                    {
                                        string definitionName = tableXmlElement.Attributes["DefinitionName"].Value.Trim();
                                        // Create DataAdapter.
                                        System.Data.IDbDataAdapter dataAdapter = CreateDataAdapter(this.dataSetType, definitionName);
                                        // Add DataAdapter.
                                        dataAdapters.Add(tableName, dataAdapter);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return dataAdapters;
        }
        #endregion
        #region Create data adapter 
        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="type">Type of data object.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(System.Type type)
        {
            return this.DataAdapterFactory.CreateDataAdapter(type);
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="type">Type of data object.</param>
        /// <param name="name">Data object name.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(System.Type type, string name)
        {
            return this.DataAdapterFactory.CreateDataAdapter(type, name);
        }

        /// <summary>
        /// Gets datatable definition name.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="type">Type of data object.</param>
        /// <param name="name">Data object name.</param>
        private void GetDataTableDefinitionName(string tableName, out System.Type type, out string name)
        {
            type = this.dataSetType;
            name = tableName;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }
            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                name = this.dataTableDefinitionNames[tableName];
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type);
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="update">addtion Update Command.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, bool update)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, update);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, update);
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, string[] columnNames)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, columnNames);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, columnNames);
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, object[] primaryKeys)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, primaryKeys);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, primaryKeys);
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, object[] primaryKeys, string[] columnNames)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, primaryKeys, columnNames);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, primaryKeys, columnNames);
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="update">addtion Update Command.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, string[] columnNames, bool update)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, columnNames, update);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, columnNames, update);
            }
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, CommonCommand conditionCommand, bool update)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, conditionCommand, update);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, conditionCommand, update);
            }
        }

        /// <summary>
        /// Create data adapter 
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNames">An array of Columns.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, string[] columnNames, CommonCommand conditionCommand, bool update)
        {
            System.Type type = this.dataSetType;
            if (this.dataTableTypes.ContainsKey(tableName))
            {
                type = this.dataTableTypes[tableName];
            }

            if (this.dataTableDefinitionNames.ContainsKey(tableName))
            {
                string name = this.dataTableDefinitionNames[tableName];
                return this.DataAdapterFactory.CreateDataAdapter(type, name, columnNames, conditionCommand, update);
            }
            else
            {
                return this.DataAdapterFactory.CreateDataAdapter(type, columnNames, conditionCommand, update);
            }
        }
        #endregion
        #region Create Command
        /// <summary>
        /// Create a command builder.
        /// </summary>
        /// <returns>Command builder.</returns>
        public virtual RaisingStudio.Data.Common.Builders.DataSetCommandBuilder CreateCommandBuilder()
        {
            DatabaseType databaseType = GetDatabaseType();
            if (this.definitionName != null)
            {
                return new RaisingStudio.Data.Common.Builders.DataSetCommandBuilder(this.connection, databaseType, this._providerName, this.dataSetType, this.definitionName);
            }
            else
            {
                return new RaisingStudio.Data.Common.Builders.DataSetCommandBuilder(this.connection, databaseType, this._providerName, this.dataSetType);
            }
        }

        public virtual DatabaseType GetDatabaseType()
        {
            if ((this._databaseType == DatabaseType.OTHER) && (!ProviderManager.Instance.ProviderExists(this._providerName)))
            {
                DatabaseType databaseType = ConnectionFactory.GetDatabaseType(this.connection);
                return databaseType;
            }
            return this._databaseType;
        }
        #endregion
        #region Create dataset 
        /// <summary>
        /// Create dataset.
        /// </summary>
        /// <returns>dataset.</returns>
        private System.Data.DataSet CreateDataSetInternal()
        {
            if (this.dataSetType.IsSubclassOf(typeof(System.Data.DataSet)))
            {
                System.Data.DataSet dataSet = System.Activator.CreateInstance(this.dataSetType) as System.Data.DataSet;
                return dataSet;
            }
            else
            {
                System.Data.DataSet dataSet = new System.Data.DataSet();
                System.Xml.XmlElement xmlElement = DefinitionManager.Instance.LoadXmlDocument(this.dataSetType);
                RaisingStudio.Data.DataSet.InitDataSet(dataSet, this.dataSetType, xmlElement, true);
                return dataSet;
            }
        }

        protected System.Data.DataSet defaultDataSet;
        /// <summary>
        /// default dataset.
        /// </summary>
        public System.Data.DataSet DefaultDataSet
        {
            get
            {
                if (this.defaultDataSet == null)
                {
                    this.defaultDataSet = CreateDataSetInternal();
                }
                return this.defaultDataSet;
            }
        }

        /// <summary>
        /// Create dataset.
        /// </summary>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet CreateDataSet()
        {
            if (this.cloneDataSet)
            {
                return this.DefaultDataSet.Clone();
            }
            else
            {
                return CreateDataSetInternal();
            }
        }
        #endregion

        #region Transactions
        /// <summary>
        /// Begin Database transaction.
        /// </summary>
        /// <returns>The ID of transaction.</returns>
        public virtual string BeginTransaction()
        {
            return this.transactionManager.BeginTransaction();
        }

        /// <summary>
        /// Begin Database transaction.
        /// </summary>
        /// <param name="isolationLevel">Specifies the isolation level for the transaction.</param>
        /// <returns>The ID of transaction.</returns>
        public virtual string BeginTransaction(System.Data.IsolationLevel isolationLevel)
        {
            return this.transactionManager.BeginTransaction(isolationLevel);
        }

        /// <summary>
        /// Commit Database transaction.
        /// </summary>
        public virtual void CommitTransaction()
        {
            this.transactionManager.CommitTransaction();
        }

        /// <summary>
        /// Commit Database transaction.
        /// </summary>
        /// <param name="transactionID">The ID of transaction.</param>
        public virtual void CommitTransaction(string transactionID)
        {
            this.transactionManager.CommitTransaction(transactionID);
        }

        /// <summary>
        /// Rollback transaction.
        /// </summary>
        public virtual void RollbackTransaction()
        {
            this.transactionManager.RollbackTransaction();
        }

        /// <summary>
        /// Rollback transaction.
        /// </summary>
        /// <param name="transactionID">The ID of transaction.</param>
        public virtual void RollbackTransaction(string transactionID)
        {
            this.transactionManager.RollbackTransaction(transactionID);
        }
        #endregion

        #region protected methods
        #region Base Function 
        /// <summary>
        /// Execute SQL command.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows affected.</returns>
        protected virtual int ExecuteNonQueryCommand(CommonCommand commonCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbCommand command = this.CommandBuilder.GetMappingCommand(commonCommand);
            if (transaction != null)
            {
                command.Transaction = transaction;
            }
            WriteLog(command);
            int value = RaisingStudio.Data.Common.Database.ExecuteNonQueryCommand(command);
            // set out parameter value.
            CommandConverter.FeedbackParameters(ref commonCommand, command);
            return value;
        }

        /// <summary>
        /// Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty.</returns>
        protected virtual object ExecuteScalarCommand(CommonCommand commonCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbCommand command = this.CommandBuilder.GetMappingCommand(commonCommand);
            if (transaction != null)
            {
                command.Transaction = transaction;
            }
            WriteLog(command);
            object value = RaisingStudio.Data.Common.Database.ExecuteScalarCommand(command);
            // set out parameter value.
            CommandConverter.FeedbackParameters(ref commonCommand, command);
            return value;
        }

        /// <summary>
        /// Execute the Command to the Connection and builds a DbDataReader.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>A DbDataReader object.</returns>
        protected virtual System.Data.IDataReader ExecuteReaderCommand(CommonCommand commonCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbCommand command = this.CommandBuilder.GetMappingCommand(commonCommand);
            if (transaction != null)
            {
                command.Transaction = transaction;
            }
            WriteLog(command);
            System.Data.IDataReader dataReader = RaisingStudio.Data.Common.Database.ExecuteReaderCommand(command);
            // set out parameter value.
            CommandConverter.FeedbackParameters(ref commonCommand, command);
            return dataReader;
        }
        #endregion

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetData(System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            Fill(dataSet, transaction);
            return dataSet;
        }

        public virtual int FillDataTableByDataAdapter(System.Data.IDbDataAdapter dataAdapter, System.Data.DataTable dataTable)
        {
            if (dataAdapter is System.Data.Common.DbDataAdapter)
            {
                WriteLog(dataAdapter);
                return (dataAdapter as System.Data.Common.DbDataAdapter).Fill(dataTable);
            }
            else
            {
                // TODO: exception.
                return -1;
            }
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;
            foreach (string tableName in this.DefaultDataAdapters.Keys)
            {
                System.Data.IDbDataAdapter dataAdapter = this.DefaultDataAdapters[tableName];
                dataAdapter.SelectCommand.Transaction = transaction;
                count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);                
            }
            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }

        public virtual int UpdateDataSetByDataAdapter(System.Data.IDbDataAdapter dataAdapter, System.Data.DataSet dataSet)
        {
            WriteLog(dataAdapter);
            return dataAdapter.Update(dataSet);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        protected virtual int Update(System.Data.DataSet dataSet, System.Data.IDbTransaction transaction)
        {
            string[] tableNames = this.CommandBuilder.CommonCommandBuilder.GetSortedTableNames();
            int count = 0;
            foreach (string tableName in tableNames)
            {
                System.Data.IDbDataAdapter dataAdapter = this.DefaultDataAdapters[tableName];
                dataAdapter.DeleteCommand.Transaction = transaction;
                dataAdapter.InsertCommand.Transaction = transaction;
                dataAdapter.UpdateCommand.Transaction = transaction;
                count += UpdateDataSetByDataAdapter(dataAdapter, dataSet);
            }
            return count;
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetData(string[] tableNames, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            Fill(dataSet, tableNames, transaction);
            return dataSet;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, string[] tableNames, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;
            foreach (string tableName in tableNames)
            {
                System.Data.IDbDataAdapter dataAdapter = this.DefaultDataAdapters[tableName];
                dataAdapter.SelectCommand.Transaction = transaction;
                count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
            }
            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        protected virtual int Update(System.Data.DataSet dataSet, string[] tableNames, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            foreach (string tableName in this.CommandBuilder.CommonCommandBuilder.GetSortedTableNames(tableNames))
            {
                System.Data.IDbDataAdapter dataAdapter = this.DefaultDataAdapters[tableName];
                dataAdapter.DeleteCommand.Transaction = transaction;
                dataAdapter.InsertCommand.Transaction = transaction;
                dataAdapter.UpdateCommand.Transaction = transaction;
                count += UpdateDataSetByDataAdapter(dataAdapter, dataSet);
            }
            return count;
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetData(Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            Fill(dataSet, columnNamesDictionary, transaction);
            return dataSet;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;
            foreach (string tableName in this.DefaultDataAdapters.Keys)
            {
                System.Data.IDbDataAdapter dataAdapter;
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], false);
                }
                else
                {
                    dataAdapter = this.DefaultDataAdapters[tableName];
                }
                dataAdapter.SelectCommand.Transaction = transaction;
                count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
            }
            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        protected virtual int Update(System.Data.DataSet dataSet, Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            string[] tableNames = this.CommandBuilder.CommonCommandBuilder.GetSortedTableNames();
            int count = 0;
            foreach (string tableName in tableNames)
            {
                System.Data.IDbDataAdapter dataAdapter;
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], true);
                }
                else
                {
                    dataAdapter = this.DefaultDataAdapters[tableName];
                }
                dataAdapter.DeleteCommand.Transaction = transaction;
                dataAdapter.InsertCommand.Transaction = transaction;
                dataAdapter.UpdateCommand.Transaction = transaction;
                count += UpdateDataSetByDataAdapter(dataAdapter, dataSet);
            }
            return count;
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetData(string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            Fill(dataSet, tableNames, columnNamesDictionary, transaction);
            return dataSet;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;
            foreach (string tableName in tableNames)
            {
                System.Data.IDbDataAdapter dataAdapter;
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], false);
                }
                else
                {
                    dataAdapter = this.DefaultDataAdapters[tableName];
                }
                dataAdapter.SelectCommand.Transaction = transaction;
                count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
            }
            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        protected virtual int Update(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            foreach (string tableName in this.CommandBuilder.CommonCommandBuilder.GetSortedTableNames(tableNames))
            {
                System.Data.IDbDataAdapter dataAdapter;
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], true);
                }
                else
                {
                    dataAdapter = this.DefaultDataAdapters[tableName];
                }
                dataAdapter.DeleteCommand.Transaction = transaction;
                dataAdapter.InsertCommand.Transaction = transaction;
                dataAdapter.UpdateCommand.Transaction = transaction;
                count += UpdateDataSetByDataAdapter(dataAdapter, dataSet);
            }
            return count;
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommands">Condition Command dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetData(string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, Dictionary<string, CommonCommand> conditionCommands, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            Fill(dataSet, tableNames, columnNamesDictionary, conditionCommands, transaction);
            return dataSet;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommands">Condition Command dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, Dictionary<string, CommonCommand> conditionCommands, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;
            foreach (string tableName in tableNames)
            {
                System.Data.IDbDataAdapter dataAdapter;
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    if (conditionCommands.ContainsKey(tableName))
                    {
                        dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], conditionCommands[tableName], false);
                    }
                    else
                    {
                        dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], false);
                    }
                }
                else if (conditionCommands.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, conditionCommands[tableName], false);
                }
                else
                {
                    dataAdapter = this.DefaultDataAdapters[tableName];
                }
                dataAdapter.SelectCommand.Transaction = transaction;
                count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
            }
            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }

        #region 自definition adapter 
        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="selectCommand">select Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, CommonCommand selectCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbDataAdapter dataAdapter = this.DataAdapterFactory.CreateEmptyDataAdapter();
            dataAdapter.SelectCommand = this.CommandBuilder.GetMappingCommand(selectCommand);
            if (transaction != null)
            {
                dataAdapter.SelectCommand.Transaction = transaction;
            }
            (dataAdapter as System.Data.IDataAdapter).TableMappings.Add(CopyTableMapping(this.DefaultDataAdapters[tableName].TableMappings[0] as System.Data.Common.DataTableMapping));
            return dataAdapter;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="selectCommand">select Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, string tableName, CommonCommand selectCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbDataAdapter dataAdapter = CreateDataAdapter(tableName, selectCommand, transaction);
            return FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
        }
        #endregion

        #region Relation Operation 
        /// <summary>
        /// append child Relation definition.
        /// </summary>
        /// <param name="dataRelationDefinitions">table relateion definition.</param>
        /// <param name="tableName">parent table name.</param>
        /// <param name="childRelations">child Relation definition column table.</param>
        public void AppenChildRelationDefinition(Dictionary<string, DataRelationDefinition> dataRelationDefinitions, string tableName, ref List<string> childRelations)
        {
            List<DataRelationDefinition> appendedRelationDefinitions = new List<DataRelationDefinition>();
            foreach (string relationName in dataRelationDefinitions.Keys)
            {
                DataRelationDefinition dataRelationDefinition = dataRelationDefinitions[relationName];
                if (dataRelationDefinition.ParentTable == tableName)
                {
                    // child Relation 。
                    childRelations.Add(relationName);

                    appendedRelationDefinitions.Add(dataRelationDefinition);
                }
            }
            foreach (DataRelationDefinition dataRelationDefinition in appendedRelationDefinitions)
            {
                string childTableName = dataRelationDefinition.ChildTable;
                AppenChildRelationDefinition(dataRelationDefinitions, childTableName, ref childRelations);
            }
        }

        /// <summary>
        /// append parent Relation definition.
        /// </summary>
        /// <param name="dataRelationDefinitions">table relateion definition.</param>
        /// <param name="tableName">child table name.</param>
        /// <param name="childRelations">parent Relation definition column table.</param>
        public void AppenParentRelationDefinition(Dictionary<string, DataRelationDefinition> dataRelationDefinitions, string tableName, ref List<string> parentRelations)
        {
            List<DataRelationDefinition> appendedRelationDefinitions = new List<DataRelationDefinition>();
            foreach (string relationName in dataRelationDefinitions.Keys)
            {
                DataRelationDefinition dataRelationDefinition = dataRelationDefinitions[relationName];
                if (dataRelationDefinition.ChildTable == tableName)
                {
                    // parent Relation 。
                    parentRelations.Add(relationName);

                    appendedRelationDefinitions.Add(dataRelationDefinition);
                }
            }
            foreach (DataRelationDefinition dataRelationDefinition in appendedRelationDefinitions)
            {
                string parentTableName = dataRelationDefinition.ParentTable;
                AppenParentRelationDefinition(dataRelationDefinitions, parentTableName, ref parentRelations);
            }
        }

        /// <summary>
        /// oder the table relations.
        /// </summary>
        /// <param name="dataRelationDefinitions">table relateion definition.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="parentRelations">parent Relation definition column table.</param>
        /// <param name="childRelations">child Relation definition column table.</param>
        public void SortDataRelationDefinitions(Dictionary<string, DataRelationDefinition> dataRelationDefinitions, string tableName, out List<string> parentRelations, out List<string> childRelations)
        {
            parentRelations = new List<string>();
            childRelations = new List<string>();
            foreach (string relationName in dataRelationDefinitions.Keys)
            {
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                if (dataRelation.ParentTable == tableName)
                {
                    // child Relation 。
                    childRelations.Add(relationName);

                    string childTableName = dataRelation.ChildTable;
                    AppenChildRelationDefinition(dataRelationDefinitions, childTableName, ref childRelations);
                }
                if (dataRelation.ChildTable == tableName)
                {
                    // parent Relation 。
                    parentRelations.Add(relationName);

                    string parentTableName = dataRelation.ParentTable;
                    AppenParentRelationDefinition(dataRelationDefinitions, parentTableName, ref parentRelations);
                }
            }
        }

        /// <summary>
        /// Create Relation table DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="fullTable">check if all data.</param>
        /// <returns>DataAdapter.</returns>
        protected virtual System.Data.IDbDataAdapter CreateRelationDataAdapter(string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, out bool fullTable)
        {
            fullTable = false;
            System.Data.IDbDataAdapter dataAdapter = null;
            foreach (DataRelationDefinition dataRelation in this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions.Values)
            {
                if ((dataRelation.ParentTable == tableName) && (dataRelation.ChildTable == tableName))
                {
                    // 自返table ，取all data.
                    if (columnNamesDictionary.ContainsKey(tableName))
                    {
                        dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName]);
                    }
                    else
                    {
                        dataAdapter = CreateDataAdapter(tableName);
                    }
                    fullTable = true;
                    return dataAdapter;
                }
            }
            if (!fullTable)
            {
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, primaryKeys, columnNamesDictionary[tableName]);
                }
                else
                {
                    dataAdapter = CreateDataAdapter(tableName, primaryKeys);
                }
            }
            return dataAdapter;
        }

        /// <summary>
        /// Create Relation table DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="fullTable">check if all data.</param>
        /// <returns>DataAdapter.</returns>
        protected virtual System.Data.IDbDataAdapter CreateRelationDataAdapter(string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, out bool fullTable)
        {
            fullTable = false;
            System.Data.IDbDataAdapter dataAdapter = null;
            foreach (DataRelationDefinition dataRelation in this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions.Values)
            {
                if ((dataRelation.ParentTable == tableName) && (dataRelation.ChildTable == tableName))
                {
                    // 自返table ，取all data.
                    if (columnNamesDictionary.ContainsKey(tableName))
                    {
                        dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName]);
                    }
                    else
                    {
                        dataAdapter = CreateDataAdapter(tableName);
                    }
                    fullTable = true;
                    return dataAdapter;
                }
            }
            if (!fullTable)
            {
                if (columnNamesDictionary.ContainsKey(tableName))
                {
                    dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName], conditionCommand, false);
                }
                else
                {
                    dataAdapter = CreateDataAdapter(tableName, conditionCommand, false);
                }
            }
            return dataAdapter;
        }

        /// <summary>
        /// Create DataAdapter.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="selectCommand">select Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>DataAdapter.</returns>
        public virtual System.Data.IDbDataAdapter CreateDataAdapter(string tableName, System.Data.IDbCommand selectCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbDataAdapter dataAdapter = this.DataAdapterFactory.CreateEmptyDataAdapter();
            dataAdapter.SelectCommand = selectCommand;
            if (transaction != null)
            {
                dataAdapter.SelectCommand.Transaction = transaction;
            }
            (dataAdapter as System.Data.IDataAdapter).TableMappings.Add(CopyTableMapping(this.DefaultDataAdapters[tableName].TableMappings[0] as System.Data.Common.DataTableMapping));
            return dataAdapter;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="selectCommand">select Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, string tableName, System.Data.IDbCommand selectCommand, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbDataAdapter dataAdapter = CreateDataAdapter(tableName, selectCommand, transaction);
            return FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int Fill(System.Data.DataSet dataSet, string tableName, Dictionary<string, string[]> columnNamesDictionary, System.Data.IDbTransaction transaction)
        {
            System.Data.IDbDataAdapter dataAdapter = null;
            if (columnNamesDictionary.ContainsKey(tableName))
            {
                dataAdapter = CreateDataAdapter(tableName, columnNamesDictionary[tableName]);
            }
            else
            {
                dataAdapter = CreateDataAdapter(tableName);
            }
            dataAdapter.SelectCommand.Transaction = transaction;
            return FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetDataByRelation(string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, bool viewMode, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            FillByRelation(dataSet, tableName, primaryKeys, columnNamesDictionary, viewMode, transaction);
            return dataSet;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, bool viewMode, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;

            bool fullTable;
            System.Data.IDbDataAdapter dataAdapter = CreateRelationDataAdapter(tableName, primaryKeys, columnNamesDictionary, out fullTable);
            dataAdapter.SelectCommand.Transaction = transaction;
            count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);

            Dictionary<string, DataRelationDefinition> dataRelationDefinitions = this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions;
            List<string> parentRelations;
            List<string> childRelations;
            SortDataRelationDefinitions(dataRelationDefinitions, tableName, out parentRelations, out childRelations);
            
            Dictionary<string, CommonCommand> dataTableCommands = new Dictionary<string, CommonCommand>();
            #region 取parent data 
            foreach (string relationName in parentRelations)
            {
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                string parentTableName = dataRelation.ParentTable;
                // 取parent data.
                if (fullTable)
                {
                    // 取全table.
                    count += Fill(dataSet, parentTableName, columnNamesDictionary, transaction);
                }
                else
                {
                    if (viewMode)
                    {
                        string childTableName = dataRelation.ChildTable;
                        if (childTableName == tableName)
                        {
                            System.Data.IDbCommand parentSelectCommand;
                            if (columnNamesDictionary.ContainsKey(parentTableName))
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentSelectCommand(dataRelation, columnNamesDictionary[parentTableName], primaryKeys);
                            }
                            else
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentSelectCommand(dataRelation, primaryKeys);
                            }
                            // Sets parameters.
                            for (int i = 0; i < parentSelectCommand.Parameters.Count; i++)
                            {
                                SetParameterValue(parentSelectCommand.Parameters[i], primaryKeys[i]);
                            }
                            System.Data.IDbDataAdapter parentDataAdapter = CreateDataAdapter(parentTableName, parentSelectCommand, transaction);
                            parentDataAdapter.SelectCommand.Transaction = transaction;
                            count += FillDataTableByDataAdapter(parentDataAdapter, dataSet.Tables[parentTableName]);

                            // Gets Relation command text.
                            if (!dataTableCommands.ContainsKey(parentTableName))
                            {
                                CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetParentRelationCommand(dataRelation, primaryKeys);
                                relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                                dataTableCommands.Add(parentTableName, relationCommand);
                            }
                        }
                        else
                        {
                            CommonCommand childCondition = dataTableCommands[childTableName];
                            System.Data.IDbCommand parentSelectCommand;
                            if (columnNamesDictionary.ContainsKey(parentTableName))
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentCommand(dataRelation, columnNamesDictionary[parentTableName], null, childCondition, false);
                            }
                            else
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentCommand(dataRelation, null, childCondition, false);
                            }
                            // Sets parameters.
                            for (int i = 0; i < parentSelectCommand.Parameters.Count; i++)
                            {
                                SetParameterValue(parentSelectCommand.Parameters[i], primaryKeys[i]);
                            }
                            System.Data.IDbDataAdapter parentDataAdapter = CreateDataAdapter(parentTableName, parentSelectCommand, transaction);
                            parentDataAdapter.SelectCommand.Transaction = transaction;
                            count += FillDataTableByDataAdapter(parentDataAdapter, dataSet.Tables[parentTableName]);

                            // Gets Relation command text.
                            if (!dataTableCommands.ContainsKey(parentTableName))
                            {
                                CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetParentRelationCommand(dataRelation, null, childCondition, false);
                                relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                                dataTableCommands.Add(parentTableName, relationCommand);
                            }
                        }
                    }
                    else
                    {
                        // 取全table.
                        count += Fill(dataSet, parentTableName, columnNamesDictionary, transaction);
                    }
                }
            }
            #endregion
            #region 取child data 
            foreach (string relationName in childRelations)
            {
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                string childTableName = dataRelation.ChildTable;
                // 取child data.
                if (fullTable)
                {
                    // 取全table.
                    count += Fill(dataSet, childTableName, columnNamesDictionary, transaction);
                }
                else
                {
                    string parentTableName = dataRelation.ParentTable;
                    if (parentTableName == tableName)
                    {
                        System.Data.IDbCommand childSelectCommand;
                        if (columnNamesDictionary.ContainsKey(childTableName))
                        {
                            childSelectCommand = this.CommandBuilder.GetChildSelectCommand(dataRelation, columnNamesDictionary[childTableName], primaryKeys);
                        }
                        else
                        {
                            childSelectCommand = this.CommandBuilder.GetChildSelectCommand(dataRelation, primaryKeys);
                        }
                        // Sets parameters.
                        for (int i = 0; i < childSelectCommand.Parameters.Count; i++)
                        {
                            SetParameterValue(childSelectCommand.Parameters[i], primaryKeys[i]);
                        }
                        System.Data.IDbDataAdapter childDataAdapter = CreateDataAdapter(childTableName, childSelectCommand, transaction);
                        childDataAdapter.SelectCommand.Transaction = transaction;
                        count += FillDataTableByDataAdapter(childDataAdapter, dataSet.Tables[childTableName]);

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, primaryKeys);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                    else
                    {
                        CommonCommand parentCondition = dataTableCommands[parentTableName];
                        System.Data.IDbCommand childSelectCommand;
                        if (columnNamesDictionary.ContainsKey(childTableName))
                        {
                            childSelectCommand = this.CommandBuilder.GetChildCommand(dataRelation, columnNamesDictionary[parentTableName], parentCondition, null, false);
                        }
                        else
                        {
                            childSelectCommand = this.CommandBuilder.GetChildCommand(dataRelation, parentCondition, null, false);
                        }
                        // Sets parameters.
                        for (int i = 0; i < childSelectCommand.Parameters.Count; i++)
                        {
                            SetParameterValue(childSelectCommand.Parameters[i], primaryKeys[i]);
                        }
                        System.Data.IDbDataAdapter childDataAdapter = CreateDataAdapter(childTableName, childSelectCommand, transaction);
                        childDataAdapter.SelectCommand.Transaction = transaction;
                        count += FillDataTableByDataAdapter(childDataAdapter, dataSet.Tables[childTableName]);

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, parentCondition, null, false);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                }
            }
            #endregion

            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>dataset.</returns>
        protected virtual System.Data.DataSet GetDataByRelation(string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, bool viewMode, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            FillByRelation(dataSet, tableName, columnNamesDictionary, conditionCommand, viewMode, transaction);
            return dataSet;
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        protected virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, bool viewMode, System.Data.IDbTransaction transaction)
        {
            int count = 0;
            bool enforceConstraints = dataSet.EnforceConstraints;
            dataSet.EnforceConstraints = false;

            bool fullTable;
            string conditionCommandText = conditionCommand.CommandText;
            conditionCommand.CommandText = string.Format("WHERE {0}", conditionCommandText);
            System.Data.IDbDataAdapter dataAdapter = CreateRelationDataAdapter(tableName, columnNamesDictionary, conditionCommand, out fullTable);
            dataAdapter.SelectCommand.Transaction = transaction;
            count += FillDataTableByDataAdapter(dataAdapter, dataSet.Tables[tableName]);

            conditionCommand.CommandText = conditionCommandText;
            Dictionary<string, DataRelationDefinition> dataRelationDefinitions = this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions;
            List<string> parentRelations;
            List<string> childRelations;
            SortDataRelationDefinitions(dataRelationDefinitions, tableName, out parentRelations, out childRelations);

            Dictionary<string, CommonCommand> dataTableCommands = new Dictionary<string, CommonCommand>();
            #region 取parent data 
            foreach (string relationName in parentRelations)
            {
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                string parentTableName = dataRelation.ParentTable;
                // 取parent data.
                if (fullTable)
                {
                    // 取全table.
                    count += Fill(dataSet, parentTableName, columnNamesDictionary, transaction);
                }
                else
                {
                    if (viewMode)
                    {
                        string childTableName = dataRelation.ChildTable;
                        if (childTableName == tableName)
                        {
                            System.Data.IDbCommand parentSelectCommand;
                            if (columnNamesDictionary.ContainsKey(parentTableName))
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentSelectCommand(dataRelation, columnNamesDictionary[parentTableName], conditionCommand);
                            }
                            else
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentSelectCommand(dataRelation, conditionCommand);
                            }
                            System.Data.IDbDataAdapter parentDataAdapter = CreateDataAdapter(parentTableName, parentSelectCommand, transaction);
                            parentDataAdapter.SelectCommand.Transaction = transaction;
                            count += FillDataTableByDataAdapter(parentDataAdapter, dataSet.Tables[parentTableName]);

                            // Gets Relation command text.
                            if (!dataTableCommands.ContainsKey(parentTableName))
                            {
                                CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetParentRelationCommand(dataRelation, conditionCommand);
                                relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                                dataTableCommands.Add(parentTableName, relationCommand);
                            }
                        }
                        else
                        {
                            CommonCommand childCondition = dataTableCommands[childTableName];
                            System.Data.IDbCommand parentSelectCommand;
                            if (columnNamesDictionary.ContainsKey(parentTableName))
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentCommand(dataRelation, columnNamesDictionary[parentTableName], null, childCondition, false);
                            }
                            else
                            {
                                parentSelectCommand = this.CommandBuilder.GetParentCommand(dataRelation, null, childCondition, false);
                            }
                            System.Data.IDbDataAdapter parentDataAdapter = CreateDataAdapter(parentTableName, parentSelectCommand, transaction);
                            parentDataAdapter.SelectCommand.Transaction = transaction;
                            count += FillDataTableByDataAdapter(parentDataAdapter, dataSet.Tables[parentTableName]);

                            // Gets Relation command text.
                            if (!dataTableCommands.ContainsKey(parentTableName))
                            {
                                CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetParentRelationCommand(dataRelation, null, childCondition, false);
                                relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                                dataTableCommands.Add(parentTableName, relationCommand);
                            }
                        }
                    }
                    else
                    {
                        // 取全table.
                        count += Fill(dataSet, parentTableName, columnNamesDictionary, transaction);
                    }
                }
            }
            #endregion
            #region 取child data 
            foreach (string relationName in childRelations)
            {
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                string childTableName = dataRelation.ChildTable;
                // 取child data.
                if (fullTable)
                {
                    // 取全table.
                    count += Fill(dataSet, childTableName, columnNamesDictionary, transaction);
                }
                else
                {
                    string parentTableName = dataRelation.ParentTable;
                    if (parentTableName == tableName)
                    {
                        System.Data.IDbCommand childSelectCommand;
                        if (columnNamesDictionary.ContainsKey(childTableName))
                        {
                            childSelectCommand = this.CommandBuilder.GetChildSelectCommand(dataRelation, columnNamesDictionary[childTableName], conditionCommand);
                        }
                        else
                        {
                            childSelectCommand = this.CommandBuilder.GetChildSelectCommand(dataRelation, conditionCommand);
                        }
                        System.Data.IDbDataAdapter childDataAdapter = CreateDataAdapter(childTableName, childSelectCommand, transaction);
                        childDataAdapter.SelectCommand.Transaction = transaction;
                        count += FillDataTableByDataAdapter(childDataAdapter, dataSet.Tables[childTableName]);

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, conditionCommand);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                    else
                    {
                        CommonCommand parentCondition = dataTableCommands[parentTableName];
                        System.Data.IDbCommand childSelectCommand;
                        if (columnNamesDictionary.ContainsKey(childTableName))
                        {
                            childSelectCommand = this.CommandBuilder.GetChildCommand(dataRelation, columnNamesDictionary[parentTableName], parentCondition, null, false);
                        }
                        else
                        {
                            childSelectCommand = this.CommandBuilder.GetChildCommand(dataRelation, parentCondition, null, false);
                        }
                        System.Data.IDbDataAdapter childDataAdapter = CreateDataAdapter(childTableName, childSelectCommand, transaction);
                        childDataAdapter.SelectCommand.Transaction = transaction;
                        count += FillDataTableByDataAdapter(childDataAdapter, dataSet.Tables[childTableName]);

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, parentCondition, null, false);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                }
            }
            #endregion

            dataSet.EnforceConstraints = enforceConstraints;
            return count;
        }


        protected virtual System.Data.DataSet GetDataByRelation(string tableName, string[] relationNames, string[] columnNames, Dictionary<string, CommonCommand> parentConditions, Dictionary<string, CommonCommand> childConditions, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            FillByRelation(dataSet, tableName, relationNames, columnNames, parentConditions, childConditions, transaction);
            return dataSet;
        }

        protected virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, string[] relationNames, string[] columnNames, Dictionary<string, CommonCommand> parentConditions, Dictionary<string, CommonCommand> childConditions, System.Data.IDbTransaction transaction)
        {
            throw new System.NotImplementedException("此方法Function 尚未实现，敬请关注！");
            Stack<string> relationNameStack = new Stack<string>();
            foreach (string relationName in relationNames)
            {

            }
            return -1;
        }

        #region Normal Functions
        #endregion

        #region Paging Operation 
        protected virtual System.Data.DataSet GetDataByRelation(string tableName, string[] relationNames, string[] columnNames, Dictionary<string, CommonCommand> parentConditions, Dictionary<string, CommonCommand> childConditions, int startRecord, int maxRecords, System.Data.IDbTransaction transaction)
        {
            System.Data.DataSet dataSet = CreateDataSet();
            FillByRelation(dataSet, tableName, relationNames, columnNames, parentConditions, childConditions, startRecord, maxRecords, transaction);
            return dataSet;
        }

        protected virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, string[] relationNames, string[] columnNames, Dictionary<string, CommonCommand> parentConditions, Dictionary<string, CommonCommand> childConditions, int startRecord, int maxRecords, System.Data.IDbTransaction transaction)
        {
            throw new System.NotImplementedException("此方法Function 尚未实现，敬请关注！");
            return -1;
        }
        #endregion

        public void SetParameterValue(object parameter, object value)
        {
            (parameter as System.Data.IDbDataParameter).Value = value;
        }

        /// <summary>
        /// has self relation.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <returns>if has return true, otherwise return false.</returns>
        public bool IsFullTableRelation(string tableName)
        {
            foreach (DataRelationDefinition dataRelation in this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions.Values)
            {
                if ((dataRelation.ParentTable == tableName) && (dataRelation.ChildTable == tableName))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        protected virtual int DeleteByRelation(string tableName, object[] primaryKeys, System.Data.IDbTransaction transaction)
        {
            // 关联Delete 。
            List<System.Data.IDbCommand> deleteCommands = new List<System.Data.IDbCommand>();
            #region create Delete Command 
            bool fullTable = IsFullTableRelation(tableName);
            System.Data.IDbCommand deleteTableCommand;
            if (fullTable)
            {
                deleteTableCommand = this.CommandBuilder.GetDeleteCommand(tableName);// Gets 整table Delete Command.
            }
            else
            {
                deleteTableCommand = this.CommandBuilder.GetDeleteCommand(tableName, primaryKeys);// Gets primaryKeysDelete Command.
            }
            deleteCommands.Add(deleteTableCommand);

            Dictionary<string, DataRelationDefinition> dataRelationDefinitions = this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions;
            List<string> parentRelations;
            List<string> childRelations;
            SortDataRelationDefinitions(dataRelationDefinitions, tableName, out parentRelations, out childRelations);

            Dictionary<string, CommonCommand> dataTableCommands = new Dictionary<string, CommonCommand>();
            #region 取child Delete Command 
            foreach (string relationName in childRelations)
            {
                System.Data.IDbCommand deleteChildCommand;
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                string childTableName = dataRelation.ChildTable;
                // 取child Delete Command.
                if (fullTable)
                {
                    // 取全table.
                    deleteChildCommand = this.CommandBuilder.GetDeleteCommand(childTableName); // Gets 整table Delete Command.
                }
                else
                {
                    string parentTableName = dataRelation.ParentTable;
                    if (parentTableName == tableName)
                    {
                        deleteChildCommand = this.CommandBuilder.GetChildDeleteCommand(dataRelation, primaryKeys);
                        // Sets parameters.
                        for (int i = 0; i < deleteChildCommand.Parameters.Count; i++)
                        {
                            SetParameterValue(deleteChildCommand.Parameters[i], primaryKeys[i]);
                        }

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, primaryKeys);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                    else
                    {
                        CommonCommand parentCondition = dataTableCommands[parentTableName];
                        
                        deleteChildCommand = this.CommandBuilder.GetDeleteChildCommand(dataRelation, parentCondition, null, false);
                        // Sets parameters.
                        for (int i = 0; i < deleteChildCommand.Parameters.Count; i++)
                        {
                            SetParameterValue(deleteChildCommand.Parameters[i], primaryKeys[i]);
                        }

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, parentCondition, null, false);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                }
                if (deleteChildCommand != null)
                {
                    deleteCommands.Add(deleteChildCommand);
                }
            }
            #endregion
            #endregion
            #region 执行Delete Command 
            int count = 0;
            for (int i = deleteCommands.Count - 1; i >= 0; i--)
            {
                System.Data.IDbCommand deleteCommand = deleteCommands[i];
                if (transaction != null)
                {
                    deleteCommand.Transaction = transaction;
                }
                count += RaisingStudio.Data.Common.Database.ExecuteNonQueryCommand(deleteCommand);// 执行Command.
            }
            #endregion
            return count;
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="transaction">Database transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        protected virtual int DeleteByRelation(string tableName, CommonCommand conditionCommand, System.Data.IDbTransaction transaction)
        {
            // 关联Delete 。
            List<System.Data.IDbCommand> deleteCommands = new List<System.Data.IDbCommand>();
            #region create Delete Command 
            bool fullTable = IsFullTableRelation(tableName);
            System.Data.IDbCommand deleteTableCommand;
            if (fullTable)
            {
                deleteTableCommand = this.CommandBuilder.GetDeleteCommand(tableName);// Gets 整table Delete Command.
            }
            else
            {
                string conditionCommandText = conditionCommand.CommandText;
                conditionCommand.CommandText = string.Format("WHERE {0}", conditionCommandText);
                deleteTableCommand = this.CommandBuilder.GetDeleteCommand(tableName, conditionCommand);// Gets Condition Delete Command.
                conditionCommand.CommandText = conditionCommandText;
            }
            deleteCommands.Add(deleteTableCommand);

            Dictionary<string, DataRelationDefinition> dataRelationDefinitions = this.CommandBuilder.CommonCommandBuilder.DataRelationDefinitions;
            List<string> parentRelations;
            List<string> childRelations;
            SortDataRelationDefinitions(dataRelationDefinitions, tableName, out parentRelations, out childRelations);

            Dictionary<string, CommonCommand> dataTableCommands = new Dictionary<string, CommonCommand>();
            #region 取child Delete Command 
            foreach (string relationName in childRelations)
            {
                System.Data.IDbCommand deleteChildCommand;
                DataRelationDefinition dataRelation = dataRelationDefinitions[relationName];
                string childTableName = dataRelation.ChildTable;
                // 取child Delete Command.
                if (fullTable)
                {
                    // 取全table.
                    deleteChildCommand = this.CommandBuilder.GetDeleteCommand(childTableName); // Gets 整table Delete Command.
                }
                else
                {
                    string parentTableName = dataRelation.ParentTable;
                    if (parentTableName == tableName)
                    {
                        deleteChildCommand = this.CommandBuilder.GetChildDeleteCommand(dataRelation, conditionCommand);

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, conditionCommand);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                    else
                    {
                        CommonCommand parentCondition = dataTableCommands[parentTableName];

                        deleteChildCommand = this.CommandBuilder.GetDeleteChildCommand(dataRelation, parentCondition, null, false);

                        // Gets Relation command text.
                        if (!dataTableCommands.ContainsKey(childTableName))
                        {
                            CommonCommand relationCommand = this.CommandBuilder.CommonCommandBuilder.GetChildRelationCommand(dataRelation, parentCondition, null, false);
                            relationCommand.CommandText = string.Format("(EXISTS ({0}))", relationCommand.CommandText);
                            dataTableCommands.Add(childTableName, relationCommand);
                        }
                    }
                }
                if (deleteChildCommand != null)
                {
                    deleteCommands.Add(deleteChildCommand);
                }
            }
            #endregion
            #endregion
            #region 执行Delete Command 
            int count = 0;
            for (int i = deleteCommands.Count - 1; i >= 0; i--)
            {
                System.Data.IDbCommand deleteCommand = deleteCommands[i];
                if (transaction != null)
                {
                    deleteCommand.Transaction = transaction;
                }
                count += RaisingStudio.Data.Common.Database.ExecuteNonQueryCommand(deleteCommand);// 执行Command.
            }
            #endregion
            return count;
        }

        protected virtual int UpdateByRelation()
        {
            // TODO: 关联Update 。
            throw new System.NotImplementedException("此方法Function 尚未实现，敬请关注！");
            return -1;
        }
        #endregion
        #endregion

        #region Common Methods
        #region Base Function 
        /// <summary>
        /// Execute SQL command.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <returns>The number of rows affected.</returns>
        public virtual int ExecuteNonQueryCommand(CommonCommand commonCommand)
        {
            return ExecuteNonQueryCommand(commonCommand, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <returns>The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty.</returns>
        public virtual object ExecuteScalarCommand(CommonCommand commonCommand)
        {
            return ExecuteScalarCommand(commonCommand, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Execute the Command to the Connection and builds a DbDataReader.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <returns>A DbDataReader object.</returns>
        public virtual System.Data.IDataReader ExecuteReaderCommand(CommonCommand commonCommand)
        {
            return ExecuteReaderCommand(commonCommand, this.TransactionManager.DefaultTransaction);
        }
        #endregion

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData()
        {
            return GetData(this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet)
        {
            return Fill(dataSet, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet)
        {
            return Update(dataSet, this.TransactionManager.DefaultTransaction);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(string[] tableNames)
        {
            return GetData(tableNames, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, string[] tableNames)
        {
            return Fill(dataSet, tableNames, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, string[] tableNames)
        {
            return Update(dataSet, tableNames, this.TransactionManager.DefaultTransaction);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(Dictionary<string, string[]> columnNamesDictionary)
        {
            return GetData(columnNamesDictionary, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, Dictionary<string, string[]> columnNamesDictionary)
        {
            return Fill(dataSet, columnNamesDictionary, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, Dictionary<string, string[]> columnNamesDictionary)
        {
            return Update(dataSet, columnNamesDictionary, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(string[] tableNames, Dictionary<string, string[]> columnNamesDictionary)
        {
            return GetData(tableNames, columnNamesDictionary, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary)
        {
            return Fill(dataSet, tableNames, columnNamesDictionary, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary)
        {
            return Update(dataSet, tableNames, columnNamesDictionary, this.TransactionManager.DefaultTransaction);
        }

        #region Relation Operation 
        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, bool viewMode)
        {
            return GetDataByRelation(tableName, primaryKeys, columnNamesDictionary, viewMode, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, bool viewMode)
        {
            return FillByRelation(dataSet, tableName, primaryKeys, columnNamesDictionary, viewMode, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, bool viewMode)
        {
            return GetDataByRelation(tableName, columnNamesDictionary, conditionCommand, viewMode, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, bool viewMode)
        {
            return FillByRelation(dataSet, tableName, columnNamesDictionary, conditionCommand, viewMode, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(string tableName, object[] primaryKeys)
        {
            return DeleteByRelation(tableName, primaryKeys, this.TransactionManager.DefaultTransaction);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(string tableName, CommonCommand conditionCommand)
        {
            return DeleteByRelation(tableName, conditionCommand, this.TransactionManager.DefaultTransaction);
        }
        #endregion
        #endregion
        #region Transaction
        #region Base Function 
        /// <summary>
        /// Execute SQL command.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows affected.</returns>
        public virtual int ExecuteNonQueryCommand(CommonCommand commonCommand, string transactionID)
        {
            return ExecuteNonQueryCommand(commonCommand, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The first column of the first row in the result set, or a null reference (Nothing in Visual Basic) if the result set is empty.</returns>
        public virtual object ExecuteScalarCommand(CommonCommand commonCommand, string transactionID)
        {
            return ExecuteScalarCommand(commonCommand, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Execute the Command to the Connection and builds a DbDataReader.
        /// </summary>
        /// <param name="commonCommand">Command.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>A DbDataReader object.</returns>
        public virtual System.Data.IDataReader ExecuteReaderCommand(CommonCommand commonCommand, string transactionID)
        {
            return ExecuteReaderCommand(commonCommand, this.TransactionManager.GetTransaction(transactionID));
        }
        #endregion

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(string transactionID)
        {
            return GetData(this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, string transactionID)
        {
            return Fill(dataSet, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, string transactionID)
        {
            return Update(dataSet, this.TransactionManager.GetTransaction(transactionID));
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(string[] tableNames, string transactionID)
        {
            return GetData(tableNames, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, string[] tableNames, string transactionID)
        {
            return Fill(dataSet, tableNames, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, string[] tableNames, string transactionID)
        {
            return Update(dataSet, tableNames, this.TransactionManager.GetTransaction(transactionID));
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(Dictionary<string, string[]> columnNamesDictionary, string transactionID)
        {
            return GetData(columnNamesDictionary, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, Dictionary<string, string[]> columnNamesDictionary, string transactionID)
        {
            return Fill(dataSet, columnNamesDictionary, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, Dictionary<string, string[]> columnNamesDictionary, string transactionID)
        {
            return Update(dataSet, columnNamesDictionary, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, string transactionID)
        {
            return GetData(tableNames, columnNamesDictionary, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, string transactionID)
        {
            return Fill(dataSet, tableNames, columnNamesDictionary, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableNames">An array of table names.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, string[] tableNames, Dictionary<string, string[]> columnNamesDictionary, string transactionID)
        {
            return Update(dataSet, tableNames, columnNamesDictionary, this.TransactionManager.GetTransaction(transactionID));
        }
        
        #region Relation Operation 
        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, bool viewMode, string transactionID)
        {
            return GetDataByRelation(tableName, primaryKeys, columnNamesDictionary, viewMode, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, object[] primaryKeys, Dictionary<string, string[]> columnNamesDictionary, bool viewMode, string transactionID)
        {
            return FillByRelation(dataSet, tableName, primaryKeys, columnNamesDictionary, viewMode, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, bool viewMode, string transactionID)
        {
            return GetDataByRelation(tableName, columnNamesDictionary, conditionCommand, viewMode, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableName">table name.</param>
        /// <param name="columnNamesDictionary">column name array dictionary.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, string tableName, Dictionary<string, string[]> columnNamesDictionary, CommonCommand conditionCommand, bool viewMode, string transactionID)
        {
            return FillByRelation(dataSet, tableName, columnNamesDictionary, conditionCommand, viewMode, this.TransactionManager.GetTransaction(transactionID));
        }
       
        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(string tableName, object[] primaryKeys, string transactionID)
        {
            return DeleteByRelation(tableName, primaryKeys, this.TransactionManager.GetTransaction(transactionID));
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableName">table name.</param>
        /// <param name="conditionCommand">Condition Command.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(string tableName, CommonCommand conditionCommand, string transactionID)
        {
            return DeleteByRelation(tableName, conditionCommand, this.TransactionManager.GetTransaction(transactionID));
        }
        #endregion
        #endregion

        #region Extension Functions
        #region Common Methods
        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(TableExpression[] tableExpressions)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            return GetData(tableNames);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, TableExpression[] tableExpressions)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            return Fill(dataSet, tableNames);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, TableExpression[] tableExpressions)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            return Update(dataSet, tableNames);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary)
        {
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return GetData(columnNamesDictionary);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary)
        {
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Fill(dataSet, columnNamesDictionary);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary)
        {
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Update(dataSet, columnNamesDictionary);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(TableExpression[] tableExpressions, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return GetData(tableNames, columnNamesDictionary);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, TableExpression[] tableExpressions, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Fill(dataSet, tableNames, columnNamesDictionary);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, TableExpression[] tableExpressions, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Update(dataSet, tableNames, columnNamesDictionary);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(RaisingStudio.Data.Expressions.TableExpression tableExpression, object[] primaryKeys, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, bool viewMode)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return GetDataByRelation(tableName, primaryKeys, columnNamesDictionary, viewMode);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, TableExpression tableExpression, object[] primaryKeys, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, bool viewMode)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return FillByRelation(dataSet, tableName, primaryKeys, columnNamesDictionary, viewMode);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="primaryKey">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelationByPrimaryKey(System.Data.DataSet dataSet, TableExpression tableExpression, object primaryKey, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, bool viewMode)
        {
            return FillByRelation(dataSet, tableExpression, new object[] { primaryKey }, columnExpressionsDictionary, viewMode);
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="conditionExpression">Condition expression</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(TableExpression tableExpression, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, ConditionExpression conditionExpression, bool viewMode)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            RaisingStudio.Data.Common.Builders.Expressions.ConditionExpressionCommandBuilder conditionExpressionCommandBuilder = CreateConditionExpressionCommandBuilder(conditionExpression, tableExpression);
            CommonCommand conditionCommand = conditionExpressionCommandBuilder.GetExpressionCommand();
            return GetDataByRelation(tableName, columnNamesDictionary, conditionCommand, viewMode);
        }

        public ConditionExpressionCommandBuilder CreateConditionExpressionCommandBuilder(ConditionExpression conditionExpression, TableExpression tableExpression)
        {
            Dictionary<string, System.Data.DbType> dbTypes = GetColumnsDbTypes();
            //TableExpression tableExpression = GetTableExpression();
            return new ConditionExpressionCommandBuilder(conditionExpression, dbTypes, tableExpression);
        }

        private Dictionary<string, System.Data.DbType> GetColumnsDbTypes()
        {
            // TODO: 
            return this.CommandBuilder.CommonCommandBuilder.DbTypes;
        }

        /*
        private TableExpression GetTableExpression()
        {
            return new TableExpression(this.DefaultDataTableMapping.DataSetTable);
        }
        */
         
        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="conditionExpression">Condition expression</param>
        /// <param name="viewMode">view mode.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, TableExpression tableExpression, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, ConditionExpression conditionExpression, bool viewMode)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            RaisingStudio.Data.Common.Builders.Expressions.ConditionExpressionCommandBuilder conditionExpressionCommandBuilder = CreateConditionExpressionCommandBuilder(conditionExpression, tableExpression);
            CommonCommand conditionCommand = conditionExpressionCommandBuilder.GetExpressionCommand();
            return FillByRelation(dataSet, tableName, columnNamesDictionary, conditionCommand, viewMode);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(TableExpression tableExpression, object[] primaryKeys)
        {
            string tableName = (string)tableExpression;
            return DeleteByRelation(tableName, primaryKeys);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="primaryKey">The value of primary key.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelationByPrimaryKey(TableExpression tableExpression, object primaryKey)
        {
            return DeleteByRelation(tableExpression, new object[] { primaryKey });
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="conditionExpression">Condition expression</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(TableExpression tableExpression, ConditionExpression conditionExpression)
        {
            string tableName = (string)tableExpression;
            RaisingStudio.Data.Common.Builders.Expressions.ConditionExpressionCommandBuilder conditionExpressionCommandBuilder = CreateConditionExpressionCommandBuilder(conditionExpression, tableExpression);
            CommonCommand conditionCommand = conditionExpressionCommandBuilder.GetExpressionCommand();
            return DeleteByRelation(tableName, conditionCommand);
        }
        #endregion
        #region Transaction
        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(TableExpression[] tableExpressions, string transactionID)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            return GetData(tableNames, transactionID);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, TableExpression[] tableExpressions, string transactionID)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            return Fill(dataSet, tableNames, transactionID);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, TableExpression[] tableExpressions, string transactionID)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            return Update(dataSet, tableNames, transactionID);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, string transactionID)
        {
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return GetData(columnNamesDictionary, transactionID);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, string transactionID)
        {
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Fill(dataSet, columnNamesDictionary, transactionID);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, string transactionID)
        {
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Update(dataSet, columnNamesDictionary, transactionID);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetData(TableExpression[] tableExpressions, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, string transactionID)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return GetData(tableNames, columnNamesDictionary, transactionID);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int Fill(System.Data.DataSet dataSet, TableExpression[] tableExpressions, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, string transactionID)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Fill(dataSet, tableNames, columnNamesDictionary, transactionID);
        }

        /// <summary>
        /// Update dataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpressions">An array of table expression.</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int Update(System.Data.DataSet dataSet, TableExpression[] tableExpressions, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, string transactionID)
        {
            string[] tableNames = ConditionExpressionCommandBuilder.GetTableNamesFromTableExpressions(tableExpressions);
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return Update(dataSet, tableNames, columnNamesDictionary, transactionID);
        }


        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(RaisingStudio.Data.Expressions.TableExpression tableExpression, object[] primaryKeys, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, bool viewMode, string transactionID)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return GetDataByRelation(tableName, primaryKeys, columnNamesDictionary, viewMode, transactionID);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, TableExpression tableExpression, object[] primaryKeys, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, bool viewMode, string transactionID)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            return FillByRelation(dataSet, tableName, primaryKeys, columnNamesDictionary, viewMode, transactionID);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="primaryKey">The value of primary key.</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelationByPrimaryKey(System.Data.DataSet dataSet, TableExpression tableExpression, object primaryKey, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, bool viewMode, string transactionID)
        {
            return FillByRelation(dataSet, tableExpression, new object[] { primaryKey }, columnExpressionsDictionary, viewMode, transactionID);
        }

        /// <summary>
        /// Gets dataset.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="conditionExpression">Condition expression</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>dataset.</returns>
        public virtual System.Data.DataSet GetDataByRelation(TableExpression tableExpression, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, ConditionExpression conditionExpression, bool viewMode, string transactionID)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            RaisingStudio.Data.Common.Builders.Expressions.ConditionExpressionCommandBuilder conditionExpressionCommandBuilder = CreateConditionExpressionCommandBuilder(conditionExpression, tableExpression);
            CommonCommand conditionCommand = conditionExpressionCommandBuilder.GetExpressionCommand();
            return GetDataByRelation(tableName, columnNamesDictionary, conditionCommand, viewMode, transactionID);
        }

        /// <summary>
        /// FillDataset.
        /// </summary>
        /// <param name="dataSet">dataset.</param>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="columnExpressionsDictionary">dictionary of column expressions.</param>
        /// <param name="conditionExpression">Condition expression</param>
        /// <param name="viewMode">view mode.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows. </returns>
        public virtual int FillByRelation(System.Data.DataSet dataSet, TableExpression tableExpression, Dictionary<RaisingStudio.Data.Expressions.TableExpression, RaisingStudio.Data.Expressions.ColumnExpression[]> columnExpressionsDictionary, ConditionExpression conditionExpression, bool viewMode, string transactionID)
        {
            string tableName = (string)tableExpression;
            Dictionary<string, string[]> columnNamesDictionary = ConditionExpressionCommandBuilder.GetColumnNamesFromColumnExpressions(columnExpressionsDictionary);
            RaisingStudio.Data.Common.Builders.Expressions.ConditionExpressionCommandBuilder conditionExpressionCommandBuilder = CreateConditionExpressionCommandBuilder(conditionExpression, tableExpression);
            CommonCommand conditionCommand = conditionExpressionCommandBuilder.GetExpressionCommand();
            return FillByRelation(dataSet, tableName, columnNamesDictionary, conditionCommand, viewMode, transactionID);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="primaryKeys">The value of primary key.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(TableExpression tableExpression, object[] primaryKeys, string transactionID)
        {
            string tableName = (string)tableExpression;
            return DeleteByRelation(tableName, primaryKeys, transactionID);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="primaryKey">The value of primary key.</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelationByPrimaryKey(TableExpression tableExpression, object primaryKey, string transactionID)
        {
            return DeleteByRelation(tableExpression, new object[] { primaryKey }, transactionID);
        }

        /// <summary>
        /// Delete the data according the condition.
        /// </summary>
        /// <param name="tableExpression">table name expression</param>
        /// <param name="conditionExpression">Condition expression</param>
        /// <param name="transactionID">The ID of transaction.</param>
        /// <returns>The count of rows that updated.</returns>
        public virtual int DeleteByRelation(TableExpression tableExpression, ConditionExpression conditionExpression, string transactionID)
        {
            string tableName = (string)tableExpression;
            RaisingStudio.Data.Common.Builders.Expressions.ConditionExpressionCommandBuilder conditionExpressionCommandBuilder = CreateConditionExpressionCommandBuilder(conditionExpression, tableExpression);
            CommonCommand conditionCommand = conditionExpressionCommandBuilder.GetExpressionCommand();
            return DeleteByRelation(tableName, conditionCommand, transactionID);
        }
        #endregion
        #endregion
    }
}
